package com.bw.repair

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.widget.TextView
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.room.Database
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_base.db.ParkDatabase
import com.bw.lib_base.mvvm.BaseActivity
import com.bw.repair.databinding.ActivityRepairBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/repair/RepairActivity")
@AndroidEntryPoint
class RepairActivity : BaseActivity<ActivityRepairBinding,RepairViewModel>() {
    val repairAdapter by lazy { RepairAdapter() }
    var flag = false//true升序，false降序
    val list = mutableListOf<RepairBeanItem>()
    var position = 0
    override fun initView() {
        viewModel.getRepair()
        binding.repairRv.apply {
            adapter = repairAdapter
            layoutManager = LinearLayoutManager(this@RepairActivity)
        }

        binding.repairOrder.setOnClickListener {
            if (flag){
//                list.sortByDescending { it.repairTime }
                repairAdapter.data.sortByDescending { it.repairTime }
                binding.repairOrderiv.setImageResource(R.drawable.ic_sort_up)
            }else{
//                list.sortBy { it.repairTime }
                repairAdapter.data.sortBy { it.repairTime }
                binding.repairOrderiv.setImageResource(R.drawable.ic_sort_down)
            }
            repairAdapter.data.clear()
            repairAdapter.addData(list)
            repairAdapter.notifyDataSetChanged()
            flag = !flag
        }

        binding.btnSearch.setOnClickListener {
            val content = binding.repairSv.text.toString()
            if (content.isNullOrEmpty()){
                viewModel.getRepair()
            }else{
                if (content.startsWith("WX")){//工单查询
                    viewModel.getRepairByNum(content)
                }else{//用户查询
                    viewModel.getRepairByName(content)
                }
            }

        }

        binding.repairSv.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if (s.isNullOrEmpty()){

                    repairAdapter.data.clear()
                    repairAdapter.addData(list)
                    repairAdapter.notifyDataSetChanged()
                }
            }

            override fun afterTextChanged(s: Editable?) {

            }
        })

        repairAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/repair/RepairDetailActivity").withString("rid",repairAdapter.getItem(position).id.toString()).navigation()
        }

        ItemTouchHelper(object :ItemTouchHelper.Callback(){
            override fun getMovementFlags(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder
            ): Int {
                val left = ItemTouchHelper.LEFT
                return makeMovementFlags(0,left)
            }

            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                return false
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                position = viewHolder.adapterPosition
                repairAdapter.data.removeAt(position)
                viewModel.deleteRepair(repairAdapter.data.get(position).id!!)
                viewModel.delete(repairAdapter.data.get(position))
//                repairAdapter.remove(layoutPosition)
//                repairAdapter.notifyDataSetChanged()
            }
        }).attachToRecyclerView(binding.repairRv)

        //点击新增报修
        binding.repairAdd.setOnClickListener {
            ARouter.getInstance().build("/repair/AddRepairActivity").navigation()
        }
    }

    override fun initData() {
        viewModel.repairData.observe(this){
            list.clear()
//            repairAdapter.data.clear()
            list.addAll(it)
//            repairAdapter.addData(it)
            viewModel.addAllRoom(list)
//            repairAdapter.notifyDataSetChanged()
        }

        viewModel.repairByNumData.observe(this){
            repairAdapter.data.clear()
            repairAdapter.addData(it)
            repairAdapter.notifyDataSetChanged()
        }

        viewModel.repairByNameData.observe(this){
            repairAdapter.data.clear()
            repairAdapter.addData(it)
            repairAdapter.notifyDataSetChanged()
        }

        viewModel.deleteRepairData.observe(this){
            if (it){
                repairAdapter.notifyItemRemoved(position)
            }
        }

        viewModel.queryRepairData.observe(this){
            list.clear()
            repairAdapter.data.clear()
            list.addAll(it)
            repairAdapter.addData(it)
            repairAdapter.notifyDataSetChanged()
        }

        viewModel.addAllRoomData.observe(this){
            ToastUtils.showLong("同步成功")
            repairAdapter.notifyDataSetChanged()
        }

        viewModel.deleteRoom.observe(this){
            ToastUtils.showLong("删除成功")
            repairAdapter.notifyItemRemoved(position)
        }
    }

    override fun onResume() {
        super.onResume()
//        viewModel.getRepair()
        viewModel.queryAll()
    }
}